$(function() {
	
	$(".dept_select").attr("selected","selected");
	$(".type_select").attr("selected","selected");
	
	var dept_id,dept_name,position_id,position_name,position_type,page_num,position_maxmember,offset,page_size;

	$("#search_position_type_select").chosen({
		no_results_text: "没有找到匹配的记录"
	});
	$("#search_dept_name_select").chosen({
		no_results_text: "没有找到匹配的记录"
	});
	
	$.ajax({
		type:"POST",
		url:"position/getSelectList",
		success: function(response) {
			var jsonData = eval(response);
			console.log(jsonData);
			
			for(var i=0;i<jsonData.depList.length;i++){
				var dep = jsonData.depList[i];
				$("#search_dept_name_select").append("<option value='"+dep.depName+"'>"+dep.depName+"</option>");
			}
			$("#search_dept_name_select").trigger("chosen:updated");
		}
	});
	
	//用户点击搜索按钮
	$(".position_search_btn").click(function(){
		close_alert();
		
		dept_id = $("#dept_id-in-searchForm").val();
		dept_name = $("#search_dept_name_select").val();
		position_id = $("#position_id-in-searchForm").val();
		position_name = $("#position_name-in-searchForm").val();
		position_type = $("#search_position_type_select").val();
		page_num = $(".page_num").val();
		offset = 0;
		
		ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num,true);
	});
	
	//发送异步搜索请求
	var ajax_position_search = function(dept_id,dept_name,position_id,position_name,position_type,page_num,refresh){
		$.ajax({
			// 发送方法
			type:"POST",
			// 发送url
			url:"position/searchPositions",
			data:{
			"dep_id_string":dept_id,
			"dep_name":dept_name,
			"position_id_string":position_id,
			"position_name":position_name,
			"position_type":position_type,
			"offset":offset
			},
			// 成果返回后
			success: function(response) {
				var jsonData = eval(response);
				console.log(offset);
				page_size = jsonData.page_size;
				if(jsonData.total_num == "0"){
					alert_div($(".alert_container"),"没有岗位信息");
					$(".position_search_container").html("");
				}
				else{
					if(refresh){
						close_alert();
					}
					ajax_load_position_search(response);
				}
			}
		});
	}
	
	//加载日报搜索列表
	var ajax_load_position_search = function(position_list_data){
		$.ajax({
			type:"get",
			url:"vmresult/position/position-search.html",
			success: function(html) {
				//console
				//console.log(position_list_data);
				$(".position_search_container").html("");
				$(".position_search_container").html(html);
				var jsonData = eval(position_list_data);
				//获取列表显示
				var listHtml = getSearchList(jsonData.response);
				$(".position_search_body").replaceWith(listHtml);
				//设置table foot
				$(".page_num").text(jsonData.page_num);
				$(".total_page").text(jsonData.total_page);
				$(".total_num").text(jsonData.total_num);
				//点击首页按钮
				$(".first_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						offset = 0;
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,1);
				});
				//点击前一页按钮
				$(".pre_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						offset = offset-page_size;
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num-1);
				});
				//点击下一页按钮
				$(".next_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						offset = offset+page_size;
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num+1);
				});
				//点击尾页按钮
				$(".last_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						offset = (total_page-1)*page_size;
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,jsonData.total_page);
				});
				//点击添加
				$(".position_create_btn").click(function(){
					close_alert();
					
					$("#add_modal").on('shown.bs.modal', function () {
						   $('.chosen-select').chosen({
							   no_results_text: "没有找到匹配的记录"
						   }); 
						 })
						 
					$("#add_modal").modal('toggle');					
					
					$(".add_type_input").val("");
					$(".add_name_input").val("");
					$(".add_position_maxmember").val("");
					$(".add_dept_name_input").html("");
					//get type list
					
					$.ajax({
						type:"POST",
						url:"position/getSelectList",
						success: function(response) {
							var jsonData = eval(response);
							$(".add_dept_name_input").html("");
							$(".add_dept_name_input").append("<option value='' class='none_dept_select-in-add' selected></option>");
							for(var i=0;i<jsonData.depList.length;i++){
								var dep = jsonData.depList[i];
								$(".add_dept_name_input").append("<option value='"+dep.depId+"'>"+dep.depName+"</option>");
							}
							$(".chosen-select").trigger("chosen:updated");
							ajax_add_position();
						}
					});
					
				});
				//点击查看岗位下员工
				$(".position_view_emp_btn").on("click",function(){
					close_alert();
					
					var position_id = $(this).attr("position_id");
					console.log(position_id);
					
//					$("#view_emp_modal").modal('show');
					
					ajax_emp_search(position_id,false);
				});
				//点击修改
				$(".position_update_btn").on("click",function(){
					close_alert();
					
					var position_id = $(this).attr("position_id");	
					console.log(position_id);
					$("#edit_modal").on('shown.bs.modal', function () {
						   $('.chzn-select').chosen({
							   no_results_text: "没有找到匹配的记录"
						   }); 
						 })
					
					$("#edit_modal").modal('show');
					
					$.ajax({
						type:"POST",
						url:"position/getSelectList",
						success: function(response) {
							var jsonData = eval(response);
							//console.log("this is add modal");
							//console.log(jsonData);
							for(var i=0;i<jsonData.depList.length;i++){
								var dep = jsonData.depList[i];
								$(".update_dept_name_input").append("<option value='"+dep.depId+"'>"+dep.depName+"</option>");
							}
							$(".chzn-select").trigger("chosen:updated");
							ajax_update_position(position_id);
						}
					});	
					
				});
				//点击删除
				$(".position_delete_btn").on("click",function(){
					close_alert();
					
					var delete_position_id = $(this).attr("position_id");
					$("#delete_modal").modal('toggle');
					$(".confirm_in_delete").on("click",function(){
						$("#delete_modal").modal('hide');
						$("#delete_modal").on('hidden.bs.modal',function (e){
							$.ajax({
								type:"POST",
								url:"position/deletePosition",
								data:{
									"position_id_string":delete_position_id
								},
								// 成果返回后
								success: function(response) {
									var jsonData = eval(response);
									if(jsonData.messageType==200){
									
										alert_div($(".alert_container"),jsonData.message);
										ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,false);
										
									}
									else {
										alert_div($(".alert_container"),jsonData.message);
									}
								}
							});
						});
						
					
					});
				});
				
				
			}
		});

	}
	
	var getSearchList = function(list){
		//console.log(list);
		var html = "<tbody class='position_search_body'> ";
						for(var i=0;i<list.length;i++){
							var temp_data = list[i];
							var posMax;
							if(temp_data.positionMax==null){
								posMax = "";
							}
							else{
								posMax = temp_data.positionMax;
							}
							html+="<tr data-ng-repeat='row in tableRow'>" +
										
										"<td>"+temp_data.positionId+"</td>" +
										"<td>"+temp_data.positionName+"</td>" +
										"<td>"+temp_data.depName+"</td>" +
										"<td>"+temp_data.positionType+"</td>" +
										"<td>"+posMax+"</td>" +
										"<td>" +
											"<div class='hidden-sm hidden-xs btn-group'>" +
													"<button class='btn btn-xs btn-success position_view_emp_btn' position_id='"+temp_data.positionId+"'>" +
														"<i class='ace-icon fa fa-search-plus bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-info position_update_btn' position_id='"+temp_data.positionId+"'>" +
														"<i class='ace-icon fa fa-pencil bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-danger position_delete_btn' position_id='"+temp_data.positionId+"'>" +
														"<i class='ace-icon fa fa-trash-o bigger-120'></i>" +
													"</button>" +
											"</div>" +
										"</td>" +
									"</tr>";
						}
		html+="</tbody>";
		return html;
	}
	
	var ajax_add_position = function(){
		$("#add_position_form").validate({
			rules : {
				add_position_id:"required",
				add_position_name:"required",
				add_position_type:"required",
				add_dept_name:"required",
				add_position_maxmember:"number"
			},
			messages : {
				add_position_id:"岗位编号不能为空",
				add_position_name:"岗位名称不能为空",
				add_position_type:"岗位类型不能为空",
				add_dept_name:"部门名称不能为空",
				add_position_maxmember:"输入数字"
			},
			errorPlacement: function (error,element) {
				if(element.parent().children('.error-message-mod').length == 0){
					error.appendTo(element.parent());
				} else {
					error.appendTo(element.parent().children('.error-message-mod'));
				}
			},
			submitHandler:function(form){
				var positionName = $(".add_name_input").val();
				var positionType = $(".add_type_input").val();
				var positionMaxmember = $(".add_maxmember_input").val();
				var deptName = $(".add_dept_name_input").val();
				$("#add_modal").modal('hide');
				$("#add_modal").on('hidden.bs.modal',function (e){
					$.ajax({
						type:"POST",
						url:"position/addPosition",
						data:{
							"position_name":positionName,
							"position_type":positionType,
							"position_maxmember_string":positionMaxmember,
							"dep_id_string":deptName
						},
						success: function(data) {
							var jsonData = eval(data);
							console.log(jsonData);
							
							if(jsonData.messageType == "200"){
								alert_div($(".alert_container"),jsonData.message);
								ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,true);
							}
							else{	
								alert_div($(".alert_container"),jsonData.message);
							}	
						}
					});
				});
				
			}   
		});
			
	}

	var ajax_update_position = function(positionId){
		
			$.ajax({
				type:"POST",
				url:"position/viewPosition",
				data:{
					"position_id_string":positionId
				},
				success: function(page_data) {
					//添加显示数据
					var jsonData = eval(page_data);
					console.log(jsonData);
					
					$(".update_id_input").val(jsonData.position_id);
					$(".update_name_input").val(jsonData.position_name);
					$(".update_type_input").val(jsonData.position_type);
					$(".update_maxmember_input").val(jsonData.position_max);
					$(".update_dept_name_input").val(jsonData.dep_id);
					
					console.log($(".update_dept_name_input"));
					console.log("lalala"+jsonData.dep_id);
					
					$("#update_position_form").validate({
						rules : {
							update_position_id:"required",
							update_position_name:"required",
							update_position_type:"required",
							update_dept_name:"required",
							update_position_maxmember:"number"
						},
						messages : {
							update_position_id:"岗位编号不能为空",
							update_position_name:"岗位名称不能为空",
							update_position_type:"岗位类型不能为空",
							update_dept_name:"部门名称不能为空",
							update_position_maxmember:"输入数字"
						},
						errorPlacement: function (error,element) {
							if(element.parent().children('.error-message-mod').length == 0){
								error.appendTo(element.parent());
							} else {
								error.appendTo(element.parent().children('.error-message-mod'));
							}
						},
						submitHandler:function(form){
							var positionName = $(".update_name_input").val();
							var positionType = $(".update_type_input").val();
							var positionMaxmember = $(".update_maxmember_input").val();
							var deptName = $(".update_dept_name_input").val();
							$("#edit_modal").modal('hide');
							$("#edit_modal").on('hidden.bs.modal',function (e){
								$.ajax({
									type:"POST",
									url:"position/updatePosition",
									data:{
										"position_id_string":positionId,
										"position_name":positionName,
										"dep_id_string":deptName,
										"position_type":positionType,
										"position_maxmember_string":positionMaxmember
									},
									success: function(data) {
										var jsonData = eval(data);
										if(jsonData.messageType == 200){
											alert_div($(".alert_container"),jsonData.message);
											ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,false);
										}
										else
											alert_div($(".alert_container"),jsonData.message);
									}
								});
							});
								
						}   
					});
					
				}
			});
	}
	
	var ajax_emp_search = function(position_id,refresh){
		$.ajax({
			// 发送方法
			type:"POST",
			// 发送url
			url:"position/viewEmpInPosition",
			data:{
			"position_id_string":position_id
			},
			// 成果返回后
			success: function(response) {
				//console.log(response);
				var jsonData = eval(response);
				if(jsonData.total_num == "0"){
					alert_div($(".alert_container"),"没有员工信息");
					$(".emp_list_container").html("");
				}
				else{
					$("#view_emp_modal").modal('show');
					if(refresh){
						close_alert();
					}
					ajax_load_emp(response);
				}
			}
		});
	}
	
	var ajax_load_emp = function(emp_list_data){
		
		var jsonData = eval(emp_list_data);
		console.log(jsonData);
		//获得列表显示
		var empInfoHtml = getEmpList(jsonData.response);
		
		console.log(empInfoHtml);
		console.log($(".emp_list_container"));
		$(".emp_list_container").empty();
		$(".emp_list_container").append(empInfoHtml);
		$(".total_num").text(jsonData.total_num);
		console.log($(".emp_list_container"));
		
	}
	
	var getEmpList = function(list){
		var html = "";
			for(var i=0;i<list.length;i++){
				var temp_data = list[i];
				console.log(temp_data.empId);
				html+="<tr data-ng-repeat='row in tableRow'>" +
							"<td>"+temp_data.empId+"</td>" +
							"<td>"+temp_data.empName+"</td>" +
						"</tr>";
			}
		return html;
	}
	
	var alert_div = function(container,msg){
		container.css("display","none");
		container.html("");
		var div = "<div class='alert_div alert alert-success alert-dismissible fade in' role='alert'>" +
						"<button type='button' class='close' data-dismiss='alert' aria-label='Close'>" +
							"<span aria-hidden='true'>×</span>" +
						"</button>" +
						"<strong class='show_ms'></strong>" +
				"</div>";
		container.html(div);
		$(".show_ms").text(msg);
		container.css("display","");
	}
	
	var close_alert = function(){
		$(".alert_container").html("");
		$(".alert_container").css("display","none");
	}
	
});


